package com.hsnn.medstgmini.base.dict.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.DicDrugnameLogDao;
import com.hsnn.medstgmini.dao.impl.HibernateSqlUtilImpl;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 通用名字典维护记录Dao
 * @author 郦东飞
 * @date 2015年11月11日
 */
@Repository
public class DicDrugnameLogDaoImpl implements DicDrugnameLogDao{
	
	@Autowired
	private HibernateSqlUtilImpl dao;
	
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	/**
	 * 
	 *@category 获取通用名字典维护记录列表
	 *@author lidongfei
	 *@date 2015年11月11日
	 *String
	 */
	@Override
	public Pagination  getDicDrugnameLogList(Pagination page){
		Session session = sessionFactory.getCurrentSession();
		String startTime = page.getConditions().get("startTime");
		String endTime = page.getConditions().get("endTime");
		String drugnameCode =page.getConditions().get("drugnameCode");
		StringBuffer sql1 = new StringBuffer();
		sql1.append("	 SELECT sql_calc_found_rows a.drugname_code drugnameCode,a.drug_name drugName,a.status status,  ")
			.append("	 s.name updUser,DATE_FORMAT(a.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime,a.log_id logId,a.detail FROM  dic_drugname_log a  ")
			.append("	 LEFT JOIN sys_user s ON s.id=a.upd_user   ")
			.append("    WHERE a.drugname_code=:drugnameCode     ");
		StringBuffer sql2 = new StringBuffer();
		sql2.append("   SELECT sql_calc_found_rows a.drugname_code drugnameCode,a.drug_name drugName,   ")
		    .append(" 	b.status status,s.name updUser,DATE_FORMAT(b.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime,b.log_id logId,b.detail  FROM  dic_drugname_log b  ")
		    .append(" 	INNER JOIN dic_drugname a ON a.drugname_code=b.drugname_code  ")
		    .append(" 	LEFT JOIN sys_user s ON s.id=b.upd_user  ")
		    .append(" 	WHERE b.drugname_code=:drugnameCode  ");
		if(StringUtils.isNotBlank(drugnameCode)){
			if(StringUtils.isNotBlank(startTime)){
				sql2.append(" and b.upd_datetime >= :startTime ");
			}
			if(StringUtils.isNotBlank(endTime)){
				sql2.append(" and b.upd_datetime <= :endTime ");
			}
			sql2.append(" order by b.upd_datetime desc");
			Query query = session.createSQLQuery(sql2.toString());
			Query queryCount = session.createSQLQuery("select found_rows() ");
			query.setString("drugnameCode", drugnameCode);
			if(StringUtils.isNotBlank(startTime)){
				query.setString("startTime", startTime+" 00:00:00");
			}
			if(StringUtils.isNotBlank(endTime)){
				query.setString("endTime", endTime+" 23:59:59");
			}
			query.setFirstResult(page.getFirstResult());
			query.setMaxResults(page.getMaxResults());
			query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			page.setRows(query.list());
			page.setRecords(Long.parseLong(queryCount.uniqueResult().toString()));
			return page;
		}else{
			if(StringUtils.isNotBlank(startTime)){
				sql1.append(" and a.upd_datetime >= :startTime ");
			}
			if(StringUtils.isNotBlank(endTime)){
				sql1.append(" and a.upd_datetime <= :endTime ");
			}
			sql1.append(" order by a.upd_datetime desc");
			Query query = session.createSQLQuery(sql1.toString());
			Query queryCount = session.createSQLQuery("select found_rows() ");
			query.setString("drugnameCode", drugnameCode);
			if(StringUtils.isNotBlank(startTime)){
				query.setString("startTime", startTime+" 00:00:00");
			}
			if(StringUtils.isNotBlank(endTime)){
				query.setString("endTime", endTime+" 23:59:59");
			}
			query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			query.setFirstResult(page.getFirstResult());
			query.setMaxResults(page.getMaxResults());
			page.setRows(query.list());
			page.setRecords(Long.parseLong(queryCount.uniqueResult().toString()));
			return page;
			
		}
	}
	
	/**
	 * 
	 *@category 获取通用名字典维护记录详情
	 *@author lidongfei
	 *@date 2015年11月11日
	 *String
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,Object>>  getDicDrugnameLogDetail(Pagination page){
		Session session = sessionFactory.getCurrentSession();
		String logId = page.getConditions().get("logId");
		StringBuffer  sql = new StringBuffer();
		sql.append(" select a.drugname_code drugnameCode,a.drug_name drugName,  ")
		   .append(" a.status status,date_format(a.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime,b.name updUser ")
		   .append(" from dic_drugname_log a left join sys_user b on b.id=a.upd_user where a.drugname_code =  ")
		   .append(" (select drugname_code from dic_drugname_log where log_id = :logId)  ")
		   .append(" and a.upd_datetime <= (select upd_datetime from dic_drugname_log where log_id = :logId) ")
		   .append(" order by a.upd_datetime desc limit 2");
		Query query = session.createSQLQuery(sql.toString());
		query.setString("logId", logId);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		return query.list();
	
	}
	
}
